package cn.hchaojie.camp.tetris;

import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

// 画布

public class Board extends Canvas {
	private static final int GRID = 20;
	
	private static final int[][] T = {
			{1, 1, 1},
			{0, 1, 0},
			{0, 0, 0}
	};

	private static final int[][] L = {
			{0, 1, 0},
			{0, 1, 0},
			{0, 1, 1}
	};
	
	public Board() {
		setWidth(600);
		setHeight(600);
		
		int sizeRow = 600 / GRID;
		int sizeCol = 600 / GRID;
		
		GraphicsContext gc = getGraphicsContext2D();
		gc.setStroke(Color.BLUE);

		// 画横线
		for (int i = 0; i < GRID; i++) {
			gc.strokeLine(0, sizeRow * i, 600, sizeRow * i);
		}
		
		// 画纵线
		for (int i = 0; i < GRID; i++) {
			gc.strokeLine(sizeCol * i, 0, sizeCol * i, 600);
		}
		
		/*
		// 从左上角到右下角画一条线
		GraphicsContext gc = getGraphicsContext2D();
		gc.setStroke(Color.BLUE);
		gc.strokeLine(0, 0, 600, 600);
		
		// 画矩形
		gc.fillRect(50, 50, 100, 50);
		*/
		
		draw(gc, sizeCol, sizeRow, 10 * sizeCol, 0, T);
		draw(gc, sizeCol, sizeRow, 10 * sizeCol, 10 * sizeRow, L);
	}
	
	public void draw(GraphicsContext gc, int width, int height, int x, int y, int[][] shape) {
		for (int i = 0; i < shape.length; i++) {
			for (int j = 0; j < shape[i].length; j++) {
				if (shape[i][j] == 1) {
					gc.fillRect(x + j * width, y + i * height, width, height);
				}
			}
		}
	}
}
